
$(function() {
	$("#dialog_bar").dialog({
		bgiframe: true, 
		modal: true, 
		width: 330, 
		autoOpen: false,
		resizeStop: function(event, ui) { 
			var width = $('#dialog_bar').dialog('option', 'width');
			var height = $('#dialog_bar').dialog('option', 'height');
			$('#dialog').html(width + "x" + height);
		},

		beforeclose: function(event, ui) {
			$('#dialog_bar').text('');
			$('#dialog_bar').dialog('option', 'height', 'auto');
		}
	});
	
});

function dialog_bar(title, xy) {
	$('#dialog_bar').dialog('option', 'buttons', {
		"关闭": function() { 
			$(this).dialog("close");
		},
		
		"打印": function() { 
			$.jPrintArea("#dialog_bar");
		}
	});
	
	var regex = /^(\d+)(x)(\d+)$/;
	if (regex.test(xy)) {
		var cc = xy.split("x");
		var width = parseInt(cc[0]);
		var height = parseInt(cc[1]);
		$('#dialog_bar').dialog('option', 'height', height);
	} else {
		var width = parseInt(xy);
	}
	
	$('#dialog_bar').dialog('option', 'title', title);
	$('#dialog_bar').dialog('option', 'width', width);

	$('#dialog_bar').dialog('open');
}

var Bar = {
	dialog_view_: function (url, title, xy) {
		$.cookie('url', url);
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"打印": function() { 
				$.jPrintArea("#dialog_view");
			},
			
			"另存": function(url) { 
				// var id = $.cookie('id_');
                // var url = 'material.php?do=Purchase.test&tpl=doc&id=' + id;
				var url = $.cookie('url') + "&tpl=xls";
				// alert(url);

                jswin(url, 'save', '800x600');
			}
		});

		dialog_view(url, title, xy);
	},
	
	line: function (title, xy, c, a) {	
		var cc = c.split(",");
		var aa = a.split(",");

		var api = new jGCharts.Api();
		var b = {	
			axis_labels : aa,
			size : '500x280',
			type : 'lc' 
		}

		b.data = eval('[' + c + ']');

		var a = api.make(b);
		
		$("#dialog_bar").html($('<img>').attr('src', a));

		dialog_bar(title, xy);
	},

	column: function (title, xy, c, a) {
		var cc = c.split(",");
		var aa = a.split(",");

		var len = cc.length;
		var s = "";
		for (var i = 0; i < len; i++) {
			if (i == 0) {
				s += "[" + cc[i] + "]";
			} else {
				s += ",[" + cc[i] + "]";
			}
		}
		
		var ww = 400 / len;
		var w1 = parseInt(ww * 0.6);
		var w2 = parseInt(ww * 0.4);

		var api = new jGCharts.Api();

	    dd = eval('[[' + c + ']]');
	    var b = {
	    		data : dd,
	    		legend : aa,
	    		// axis_labels : aa,
	    		bar_width : w1,
	    		bar_spacing : w2,
	    		size : '550x280',
	    		bg            : 'ffffff',
	    		bg_type       : 'gradient',
	    		bg_angle      : 45,
	    		bg_offset     : '999999',
	    		bg_width      : 20
	    };

		var a = api.make(b);
		$("#dialog_bar").html($('<img>').attr('src', a));

		dialog_bar(title, xy);
	},

	p: function (title, xy, c, a) {
		var cc = c.split(",");
		var aa = a.split(",");

		var s = "";
		for (var i = 0; i < cc.length; i++) {
			if (i == 0) {
				s += "[" + cc[i] + ", " + cc[i] + "]";
			} else {
				s += ",[" + cc[i] + ", " + cc[i] + "]";
			}
		}

		var api = new jGCharts.Api();

		dd = eval('[' + s + ']');

		var b = {
			data : dd,
			axis_labels : aa,
			colors : ['FFCC00'],
			size : '450x280',
		    type : 'p'
		};

		var a = api.make(b);
		
		$("#dialog_bar").html($('<img>').attr('src', a));
		dialog_bar(title, xy);
	},

	p3: function (title, xy, c, a) {
		var cc = c.split(",");
		var aa = a.split(",");

		var s = "";
		for (var i = 0; i < cc.length; i++) {
			if (i == 0) {
				s += "[" + cc[i] + ", " + cc[i] + "]";
			} else {
				s += ",[" + cc[i] + ", " + cc[i] + "]";
			}
		}

		var api = new jGCharts.Api();

		dd = eval('[' + s + ']');

		var b = {
				// title : '测试',
				data : dd,
				axis_labels : aa,
				bg        : 'ffffff',
				bg_type   : 'gradient',
				bg_angle  : 90,
				size : '450x180',
				type : 'p3'
		};

		var a = api.make(b);
		
		$("#dialog_bar").html($('<img>').attr('src', a));
		
		dialog_bar(title, xy);
	}
};
